/*



Copyright (C) 2009, Etienne Moutot <e.moutot@gmail.com>



This file is part of colorPickerWidget.



    colorPickerWidget is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.



    colorPickerWidget is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.



    You should have received a copy of the GNU General Public License

    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

*/



#include <QDebug>

#include <QPainter>

#include <QMouseEvent>

#include <QPaintEvent>

#include <QLayout>



#include "ColorPickerWidget.h"



ColorPickerWidget::ColorPickerWidget(const QString &name, QWidget *parent)

    : QFrame(parent)

{    

    m_colorTriangle = new ColorTriangle();

    connect(m_colorTriangle, SIGNAL(colorChanged(QColor)), this, SLOT(onColorChanged(QColor)));



    m_colorLabel = new ColorLabel();

    connect(m_colorLabel, SIGNAL(dblClick()), m_colorTriangle, SLOT(show()));



    m_alpha = new QDoubleSpinBox();

    m_alpha->setRange(0.0, 255.0);

    m_alpha->setSingleStep(10.0);

    m_alpha->setDecimals(0);

    connect(m_alpha, SIGNAL(valueChanged(double)), this, SLOT(onAlphaChanged(double)));



    m_nameLabel = new QLabel(name);

    QHBoxLayout *layout = new QHBoxLayout(this);

    layout->setContentsMargins(0,0,0,0);

    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);

    layout->addWidget(m_nameLabel,1, Qt::AlignRight);

    layout->addWidget(m_alpha);

    layout->addWidget(m_colorLabel);



    m_alpha->setVisible(false);



    setName(name);

}



ColorPickerWidget::~ColorPickerWidget()

{

}



QColor ColorPickerWidget::getColor() const

{

    return m_color;

}



const QString &ColorPickerWidget::getName() const

{

    return m_name;

}



void ColorPickerWidget::setName(const QString &name)

{

    m_name = name;

    m_nameLabel->setText(m_name);

    setTitle(name);

}



void ColorPickerWidget::setTitle(const QString &title)

{

    m_colorTriangle->setWindowTitle(title);

}



void ColorPickerWidget::setShowAlpha(bool show)

{

    m_alpha->setVisible(show);

}



void ColorPickerWidget::setColor(const QColor &color)

{

    m_color = color;

    m_alpha->setValue(color.alpha());

    m_colorLabel->setColor(getColor());

    m_colorTriangle->setColor(getColor());

    emit colorChanged(getColor());

}



void ColorPickerWidget::onColorChanged(const QColor &color)

{

    m_color = color;

    m_color.setAlpha(m_alpha->value());

    m_colorLabel->setColor(getColor());

    emit colorChanged(getColor());

}



void ColorPickerWidget::onAlphaChanged(double alpha)

{

    m_color.setAlpha(alpha);

    m_colorLabel->setColor(getColor());

    emit colorChanged(getColor());

}



///////////////////////////////////////////////////////////////////

ColorLabel::ColorLabel(QWidget *parent) :

    QWidget(parent)

{

    setMaximumSize(25, 25);

    setMinimumSize(25, 25);

}



void ColorLabel::setColor(const QColor &color)

{

    m_color = color;

    update();

}



void ColorLabel::paintEvent(QPaintEvent *)

{

    QPainter p(this);

    p.setPen(Qt::black);

    p.setBrush(m_color);

    p.drawRect(QRect(2, 2, width()-4, height()-4));

}



void ColorLabel::mouseReleaseEvent(QMouseEvent *event)

{

    Q_UNUSED(event)

    emit dblClick();

}


